<template>
  <div class="my-counter">
    <button type="button" class="btn btn-light" @click="decrease" :disabled="dataObjClone.goods_count <= 1">-</button>
    <input type="number" class="form-control inp" v-model.number="dataObjClone.goods_count"/>
    <button type="button" class="btn btn-light" @click="dataObjClone.goods_count++">+</button>
  </div>
</template>

<script>
/* 
1.点击+,将当前商品的数量+1 
2.点击-,将当前商品的数量-1,商品的最小的数量Wie1,当数量为1时,禁用按钮
3.输入框只能输入大于等于1的数字
*/
export default {
  props: {
    dataObj: {
      typeof: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      dataObjClone: this.dataObj,
    };
  },
  watch: {
    dataObjClone: {
      deep: true,
      handler() {
        if (this.dataObjClone.goods_count <= 1) {
          this.dataObjClone.goods_count = 1;
        }
      },
    },
  },
  methods: {
    //减少
    decrease() {
      if (this.dataObjClone.goods_count <= 1) {
        this.dataObjClone.goods_count = 1
        return
      }
      this.dataObjClone.goods_count--
    },
  },
};
</script>

<style lang="less" scoped>
.my-counter {
  display: flex;

  .inp {
    width: 45px;
    text-align: center;
    margin: 0 10px;
  }

  .btn,
  .inp {
    transform: scale(0.9);
  }
}
</style>